package com.syaccp.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.syaccp.model.TOrderDetail;
import com.syaccp.model.TOrderImage;
import com.syaccp.service.TOrderDetailService;
import com.syaccp.service.TOrderImageService;
import com.syaccp.utils.Result;
import com.syaccp.utils.imgUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Api("操作图片表")
@RestController
@CrossOrigin
@RequestMapping("/api/tOrderImage/")
public class TOrderImageController {
    @Resource
    TOrderImageService tOrderImageService;

    @ApiOperation(value = "根据订单id查询相关图片")
    @GetMapping("/queryImageByOrderId")
    public Result<List<TOrderImage>> queryImageByOrderId(Integer orderId) {
        List<TOrderImage> tOrderImages = tOrderImageService.queryImageByOrderId(orderId);
        List<TOrderImage> collect = tOrderImages.stream().peek(info -> {
            if (StringUtils.isBlank(info.getImage())) { //拼接图片前缀
                info.setImage(imgUtils.DEFAULT_PIC);
            } else {
                    info.setImage(imgUtils.setRealPicPathTwo(info.getImage()));
                if (!imgUtils.checkUrlConnection(info.getImage())) {
                    info.setImage(imgUtils.DEFAULT_PIC);
                }
            }
        }).collect(Collectors.toList());
        return Result.success(collect);
    }
}
